﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using System.Windows.Forms;
using System.Collections;
using com.lover.astd.common.model.enumer;
using com.lover.astd.common.manager;
using com.lover.astd.common;
using com.lover.astd.common.model.battle;

namespace com.lover.astd.game.ui.server.impl.battle
{
    /// <summary>
    /// 军团战服务
    /// </summary>
    class BattleServer : LogicServer
    {
        public BattleServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Battle; ServerReadableName = ConfigStrings.SR_Battle; }
        private void renderData()
        {
            if (_mainForm.dg_attack_army.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser()._all_armys;
                _mainForm.dg_attack_army.AutoGenerateColumns = false;
                _mainForm.dg_attack_army.DataSource = src;
            }
            _mainForm.dg_attack_army.Refresh();
        }
        public override void renderSettings()
        {
            renderData();
            MainForm frm = _mainForm;
            Dictionary<string, string> conf = getConfig(ServerName);
            frm.chk_defense_format.Checked = conf.ContainsKey(ConfigStrings.defense_enabled) && conf[ConfigStrings.defense_enabled].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.defense_formation)) frm.combo_defense_formation.SelectedItem = conf[ConfigStrings.defense_formation];
            else frm.combo_defense_formation.SelectedItem = "不变阵";

            frm.chk_attack_npc_enable.Checked = conf.ContainsKey(ConfigStrings.npc_enabled) && conf[ConfigStrings.npc_enabled].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.npc_reserve_token))
            {
                int npc_reserve_token = 40;
                Int32.TryParse(conf[ConfigStrings.npc_reserve_token], out npc_reserve_token);
                frm.num_attack_npc_reserve_order.Value = npc_reserve_token;
            }
            if (conf.ContainsKey(ConfigStrings.npc_ids))
            {
                string npc_idstr = conf[ConfigStrings.npc_ids];
                npc_idstr = npc_idstr.Replace(",", "");
                if (npc_idstr != null && npc_idstr.Length > 0)
                {
                    string[] npc_idstrs = npc_idstr.Split(':');
                    if (npc_idstrs.Length >= 2)
                    {
                        int npc_id = 0; string formation = "";
                        Int32.TryParse(npc_idstrs[0], out npc_id);
                        formation = npc_idstrs[1];
                        if (npc_id > 0)
                        {
                            User u = getUser();
                            foreach (Npc npc in u._all_npcs)
                            {
                                if (npc.Id == npc_id)
                                {
                                    frm.lbl_attackNpc.Text = String.Format("{0}:{1}:{2}:{3}", npc.Name, npc.ItemName, formation, npc_id);
                                    break;
                                }
                            }
                        }
                    }

                }
            }
            else frm.lbl_attackNpc.Text = "";
            if (conf.ContainsKey(ConfigStrings.npc_force_ids))
            {
                string npc_idstr = conf[ConfigStrings.npc_force_ids];
                npc_idstr = npc_idstr.Replace(",", "");
                if (npc_idstr != null && npc_idstr.Length > 0)
                {
                    string[] npc_idstrs = npc_idstr.Split(':');
                    if (npc_idstrs.Length >= 2)
                    {
                        int npc_id = 0; string formation = "";
                        Int32.TryParse(npc_idstrs[0], out npc_id);
                        formation = npc_idstrs[1];
                        if (npc_id > 0)
                        {
                            User u = getUser();
                            foreach (Npc npc in u._all_npcs)
                            {
                                if (npc.Id == npc_id)
                                {
                                    frm.lbl_attackForceNpc.Text = String.Format("{0}:{1}:{2}:{3}", npc.Name, npc.ItemName, formation, npc_id);
                                    break;
                                }
                            }
                        }
                    }

                }
            }
            else frm.lbl_attackForceNpc.Text = "";

            frm.chk_attack_army.Checked = conf.ContainsKey(ConfigStrings.army_enabled) && conf[ConfigStrings.army_enabled].ToLower().Equals("true");
            frm.chk_attack_army_only_jingyingtime.Checked = conf.ContainsKey(ConfigStrings.attack_army_only_jingyingtime) && conf[ConfigStrings.attack_army_only_jingyingtime].ToLower().Equals("true");
            frm.chk_attack_army_first.Checked = conf.ContainsKey(ConfigStrings.attack_first) && conf[ConfigStrings.attack_first].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.attack_first_interval))
            {
                int attack_first_interval = 60;
                Int32.TryParse(conf[ConfigStrings.attack_first_interval], out attack_first_interval);
                frm.num_attack_army_first_interval.Value = attack_first_interval;
            }
            if (conf.ContainsKey(ConfigStrings.army_reserve_token))
            {
                int army_reserve_token = 40;
                Int32.TryParse(conf[ConfigStrings.army_reserve_token], out army_reserve_token);
                frm.num_attack_army_reserve_order.Value = army_reserve_token;
            }
            string army_ids = "", first_army_ids = "";
            if (conf.ContainsKey(ConfigStrings.army_ids)) army_ids = conf[ConfigStrings.army_ids];
            if (conf.ContainsKey(ConfigStrings.first_army_ids)) first_army_ids = conf[ConfigStrings.first_army_ids];
            renderDgToSelected(frm.dg_attack_army, army_ids, first_army_ids);
        }
        private void renderDgToSelected(DataGridView dg, string idsToSelected, string firstIdsToSelected)
        {
            BindingSource src = dg.DataSource as BindingSource;
            List<Npc> objects_all = src.DataSource as List<Npc>;
            string[] objids_to_selected = idsToSelected.Split(',');
            string[] first_objids_to_selected = firstIdsToSelected.Split(',');
            int i = 0, j = 0, now_index = 0;
            // 首先, 先把所有的ischecked取消
            for (j = 0; j < objects_all.Count; j++)
            {
                objects_all[j].IsChecked = false;
                objects_all[j].IsChecked2 = false;
            }
            for (i = 0; i < objids_to_selected.Length; i++)
            {
                if (objids_to_selected[i] == null) continue;
                string[] subs = objids_to_selected[i].Split(':');
                if (subs.Length != 2) continue;
                string idstr = subs[0];
                int id = 0;
                if (Int32.TryParse(idstr, out id))
                {
                    for (j = 0; j < objects_all.Count; j++)
                    {
                        Npc obj = objects_all[j];
                        if (obj.Id == id)
                        {
                            obj.IsChecked = true;
                            obj.Formation = subs[1];
                            objects_all.RemoveAt(j);
                            objects_all.Insert(now_index, obj);
                            now_index++;
                            break;
                        }
                    }
                }
            }
            now_index = 0;
            for (i = 0; i < first_objids_to_selected.Length; i++)
            {
                if (first_objids_to_selected[i] == null) continue;
                string[] subs = first_objids_to_selected[i].Split(':');
                if (subs.Length != 2) continue;
                string idstr = subs[0];
                int id = 0;
                if (Int32.TryParse(idstr, out id))
                {
                    for (j = 0; j < objects_all.Count; j++)
                    {
                        Npc obj = objects_all[j];
                        if (obj.Id == id)
                        {
                            obj.IsChecked = true;
                            obj.IsChecked2 = true;
                            obj.Formation = subs[1];
                            objects_all.RemoveAt(j);
                            objects_all.Insert(now_index, obj);
                            now_index++;
                            break;
                        }
                    }
                }
            }
        }
        private string getDgSelectdIds(DataGridView dg, bool checkFirstBattle)
        {
            BindingSource src = dg.DataSource as BindingSource;
            List<Npc> objects = src.DataSource as List<Npc>;
            string selectedObjIds = "";
            int index = 0;
            for (int i = 0; i < dg.Rows.Count; i++ )
            {
                DataGridViewRow row = dg.Rows[i];
                DataGridViewCell cell = row.Cells[0];
                DataGridViewCell cell2 = row.Cells[1];
                if (checkFirstBattle)
                {
                    if ((bool)(cell.Value) && (bool)(cell2.Value))
                    {
                        Npc npc = objects[index];
                        int objId = npc.Id;
                        selectedObjIds += objId + ":" + npc.Formation + ",";
                    }
                }
                else
                {
                    if ((bool)(cell.Value) && (!(bool)(cell2.Value)))
                    {
                        Npc npc = objects[index];
                        int objId = npc.Id;
                        selectedObjIds += objId + ":" + npc.Formation + ",";
                    }
                }
                index++;
            }
            if (selectedObjIds.Length > 0) selectedObjIds = selectedObjIds.Substring(0, selectedObjIds.Length - 1);

            return selectedObjIds;
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.defense_enabled, frm.chk_defense_format.Checked.ToString());
            if (frm.combo_defense_formation.SelectedItem != null)
                conf.setConfig(ServerName, ConfigStrings.defense_formation, frm.combo_defense_formation.SelectedItem.ToString()); 
            else
                conf.setConfig(ServerName, ConfigStrings.defense_formation, "不变阵");

            conf.setConfig(ServerName, ConfigStrings.npc_enabled, frm.chk_attack_npc_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.npc_reserve_token, frm.num_attack_npc_reserve_order.Value.ToString());
            string npc_idstr = frm.lbl_attackNpc.Text;
            string[] npc_idstrs = npc_idstr.Split(':');
            if (npc_idstrs.Length >= 4) conf.setConfig(ServerName, ConfigStrings.npc_ids, String.Format("{0}:{1}", npc_idstrs[3], npc_idstrs[2]));

            npc_idstr = frm.lbl_attackForceNpc.Text;
            npc_idstrs = npc_idstr.Split(':');
            if (npc_idstrs.Length >= 4) conf.setConfig(ServerName, ConfigStrings.npc_force_ids, String.Format("{0}:{1}", npc_idstrs[3], npc_idstrs[2]));

            string army_ids = getDgSelectdIds(frm.dg_attack_army, false);
            string first_army_ids = getDgSelectdIds(frm.dg_attack_army, true);
            conf.setConfig(ServerName, ConfigStrings.army_enabled, frm.chk_attack_army.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.attack_army_only_jingyingtime, frm.chk_attack_army_only_jingyingtime.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.attack_first, frm.chk_attack_army_first.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.attack_first_interval, frm.num_attack_army_first_interval.Value.ToString());
            conf.setConfig(ServerName, ConfigStrings.army_reserve_token, frm.num_attack_army_reserve_order.Value.ToString());
            conf.setConfig(ServerName, ConfigStrings.army_ids, army_ids);
            conf.setConfig(ServerName, ConfigStrings.first_army_ids, first_army_ids);
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.defense_enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.defense_formation, "不变阵");

            conf.setConfig(ServerName, ConfigStrings.npc_enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.npc_reserve_token, "0");
            conf.setConfig(ServerName, ConfigStrings.npc_ids, "");
            conf.setConfig(ServerName, ConfigStrings.npc_force_ids, "");

            conf.setConfig(ServerName, ConfigStrings.army_enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.attack_army_only_jingyingtime, "true");
            conf.setConfig(ServerName, ConfigStrings.attack_first, "false");
            conf.setConfig(ServerName, ConfigStrings.attack_first_interval, "60");
            conf.setConfig(ServerName, ConfigStrings.army_reserve_token, "0");
            conf.setConfig(ServerName, ConfigStrings.army_ids, "");
            conf.setConfig(ServerName, ConfigStrings.first_army_ids, "");
            renderSettings();
        }
    }
}
